package com.pinyougou.show.service;

import com.pinyougou.pojo.TbSeller;
import com.pinyougou.sellergoods.service.SellerService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

public class UserDetailsServiceImp implements UserDetailsService {

    /*服务层*/
    private SellerService sellerService;

    /*提供setter注入方式*/
    public void setSellerService(SellerService sellerService) {
        this.sellerService = sellerService;
    }

    /**
     * 用户认证方法
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        System.out.println("经过用户自定义认证类，并进性用户比对");

        /*用户认证身份集合*/
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();

        grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));

        TbSeller one = sellerService.findOne(username);

        if (one != null) {

            //判断用户状态
            if ("1".equals(one.getStatus())) {

                /*返回一个用户信息类*/
                return new User(username,one.getPassword(),grantedAuthorities);

            }else {
                return null;
            }

        }else {

            return null;

        }

    }
}
